<template>
    <div class="login_container">
        <div class="login_box">
            <div class="avatar_box">
                <img src="../assets/logo.png"/>
            </div>
            <div class="form_box">
                <el-form label-width="80px" :rules="rules" ref="ruleForm" :model="formLabelAlign">
                    <el-form-item label="用户名"  prop="username">
                        <el-input v-model="formLabelAlign.username" style="width:340px"></el-input>
                    </el-form-item>
                    <el-form-item label="密码"  prop="password">
                        <el-input v-model="formLabelAlign.password" type="password"  style="width:340px"></el-input>
                    </el-form-item>
                    <el-form-item style='text-align:right;margin-right: 30px;'>
                        <el-button :loading="loading" type="primary" @click="submitForm('ruleForm')">登录</el-button>
                        <el-button @click="resetForm('ruleForm')">重置</el-button>
                    </el-form-item>
                </el-form>
            </div>
        </div>
    </div>
</template>

<script>
export default {
data() {
      return {
        loading:false,
        formLabelAlign: {
          username: 'admin',
          password:'123456'
        },
        rules: {
            username: [
                { required: true, message: '请输入账号', trigger: 'blur' }
            ],
            password:[
                { required: true, message: '请输入密码', trigger: 'blur' }
            ]
        }
      };
    },
    methods: {
      submitForm(formName) {
        this.$refs[formName].validate(async(valid) => {
            if (!valid) {
                return ;
            }
            this.loading =true;
            const {data:res}=await this.request.post('login',this.formLabelAlign)
            if(res.meta.status==200){
                this.$message({
                    message:'登录成功',
                    type:'success',
                        onClose:()=>{
                            this.loading=false;
                        }
                })
                window.sessionStorage.setItem('token',res.data.token);
                this.$router.push('/home')
            }else{
                 this.$message({
                    message:'登录失败',
                    type:'error',
                        onClose:()=>{
                            this.loading=false;
                        }
                })
            }
        });
      },
      resetForm(ruleForm) {
        this.$refs[ruleForm].resetFields();
      }
    }
}
</script>

<style lang='scss' scoped>
.login_container{
    background-color: #2b4b6b;
    height: 100%;
    .login_box{
        width: 450px;
        height: 300px;
        background-color: #fff;
        position: absolute;
        left: 50%;
        top:50%;
        transform: translate(-50%,-50%);
        .avatar_box{
            background-color: #fff;
            height: 130px;
            width: 130px;
            border-radius: 50%;
            border:1px solid #eee;
            padding: 10px;
            box-shadow: 0 0 10px #ddd;
            position: absolute;
            left: 50%;
            transform: translate(-50%,-50%);
            img{
                width: 100%;
                height: 100%;
                border-radius: 50%;
                background-color: #eee;
            }
        }
        .form_box{
            margin-top: 20%;
        }
    }
}
</style>